﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using GovernmentOfficeProject.LevelDataAccessModel;
using System.Data.SqlClient;
using GovernmentOfficeProject.SysTools;
using System.Data;

namespace GovernmentOfficeProject.LevelDataAccess
{
    public class DAL_USER_UserRole
    {
        /// <summary>
        /// 增加一条关系:一个用户对应的一个角色.
        /// </summary>
        /// <param name="userEntity">用户角色实体</param>
        /// <returns></returns>
        public bool addUserRole(Entity_USER_UserRole userRoleEntity)
        {
            try
            {
                bool flag = false;
                bool blState = false;
                StringBuilder sqlBudIns = null;
                StringBuilder sqlBudValue = null;
                sqlBudIns = new StringBuilder();
                sqlBudValue = new StringBuilder();
                sqlBudIns.Append("insert into [PER_USER_ROLE](");
                sqlBudValue.Append(" values(");
                List<SqlParameter> sprList = new List<SqlParameter>();
                if (userRoleEntity.UserID != -1)
                {
                    sqlBudIns.Append("URUserID,");
                    sqlBudValue.Append("@userID,");
                    sprList.Add(new SqlParameter("@userID", userRoleEntity.UserID));
                    blState = true;
                }
                if (userRoleEntity.RoleID  != -1)
                {
                    sqlBudIns.Append("URRoleID,");
                    sqlBudValue.Append("@roleID,");
                    sprList.Add(new SqlParameter("@roleID", userRoleEntity.RoleID));
                    blState = true;
                }
                if (userRoleEntity.RecordStatus != -1)
                {
                    sqlBudIns.Append("URRecordStatus,");
                    sqlBudValue.Append("@recordStatus,");
                    sprList.Add(new SqlParameter("@recordStatus", userRoleEntity.RecordStatus));
                    blState = true;
                }
                if (blState)
                {
                    string strSQL = sqlBudIns.ToString().Substring(0, sqlBudIns.Length - 1) + ")" + sqlBudValue.ToString().Substring(0, sqlBudValue.Length - 1) + ")";
                    SqlParameter[] values = sprList.ToArray();
                    //AppLog.Write("DAL-增加用户对应一个角色的SQL语句: " + strSQL, AppLog.LogMessageType.Debug);
                    flag = DAL_DBGM.InsertInfo(strSQL, values);
                }
                else
                {
                    //添加用户和部门关系失败:没有任何数据.
                    AppLog.Write("增加用户的角色失败:没有任何数据.", AppLog.LogMessageType.Info);
                }
                return flag;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---异常位置:DAL_USER_UserRole.addUserRole(Entity_USER_UserRole userRoleEntity)---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }
        }


        /// <summary>
        /// 根据用户ID删除对应角色的关系.
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns></returns>
        public bool deleteUserRoleByUserID(decimal userID)
        {
            try
            {
                bool flag = false;
                string strSQL = "delete [PER_USER_ROLE] where URUserID=@userID";
                SqlParameter[] values = new SqlParameter[]{
                    new SqlParameter("@userID",userID)
                };
                flag = DAL_DBGM.DeleteInfo(strSQL, values);
                return flag;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---异常位置:DAL_USER_UserRole.deleteUserRoleByUserID(decimal userID)---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }
        }

        /// <summary>
        /// 根据用户id和角色id删除对应关系.
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public bool deleteUserRoleByUserIDAndRoleID(decimal userID, decimal roleID)
        {
            try
            {
                bool flag = false;
                string strSQL = "delete [PER_USER_ROLE] where URUserID=@userID and URRoleID=@roleID";
                SqlParameter[] values = new SqlParameter[]{
                    new SqlParameter("@userID",userID),
                    new SqlParameter("@roleID",roleID)
                };
                flag = DAL_DBGM.DeleteInfo(strSQL, values);
                return flag;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---异常位置:DAL_USER_UserRole.deleteUserDeptByUserIDAndDeptID(decimal userID, decimal roleID)---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }

        }

        /// <summary>
        /// 根据角色ID,获取所属部门的用户数量.
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public int getUserCountByRoleID(decimal roleID)
        {
            try
            {
                int userCount = 0;
                string strSQL = "select * from [PER_USER_ROLE] where URRoleID=@roleID";
                SqlParameter[] values = new SqlParameter[]{
                    new SqlParameter("@roleID",roleID)
                };
                DataSet dst = DAL_DBGM.GetDataSet(strSQL, values);
                userCount = dst.Tables[0].Rows.Count;
                return userCount;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---异常位置:DAL_USER_UserDept.getUserCountByRoleID(decimal roleID)---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }

        }
    }
}
